package com.test.design.material.materialdesign;

import android.os.Bundle;
import android.widget.TableLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;

import static androidx.fragment.app.FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT;

public class ViewPagerActivity extends AppCompatActivity {
    private ViewPagerAdapter adapter;
    private String[] titles={"推荐","分类","排行"};

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_pager);

        adapter = new ViewPagerAdapter(getSupportFragmentManager(), BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,titles);
        ViewPager viewPager = findViewById(R.id.viewpager);
        TabLayout tabLayout=findViewById(R.id.tablayout);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.setTabMode(TabLayout.MODE_FIXED);
    }


    public static class ViewPagerAdapter extends FragmentPagerAdapter {

        private final String[] list_Title;

        @Override
        public CharSequence getPageTitle(int position) {
            return list_Title[position];
        }

        public ViewPagerAdapter(@NonNull FragmentManager fm, int behavior,String[] list_Title) {
            super(fm, behavior);

            this.list_Title = list_Title;
        }

        public Fragment getItem(int position) {
            if (position == 0) {
                return FragmentTest.newInstance("页面-1");
            } else if (position == 1) {
                return FragmentTest.newInstance("页面-2");
            } else if (position == 2) {
                return FragmentTest.newInstance("页面-3");
            }
            return null;
        }


        @Override
        public int getCount() {
            return 3;
        }
    }
}
